﻿package ywh.ui.courseslider
{
	import flash.events.EventDispatcher;
	import fl.containers.UILoader;
	import fl.core.InvalidationType;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.PixelSnapping;
	import flash.events.Event;
	
	/**
	 * 缩略图查看器
	 * 继承自UILoader类，
	 * 当加载内容是Bitmap的时候，修正了平滑选项;
	 * 默认缩放
	 * @author WilliWHYang
	 */
	public class CourseSliderThumbLoader extends UILoader
	{
		protected var background:DisplayObject;
		
		public function CourseSliderThumbLoader()
		{
			super();
			//thumbLoader.addEventListener(IOErrorEvent.IO_ERROR, thumbLoader_ioError);
			this.scaleContent = true;
		}
		protected static var defaultStyles:Object = {ywhThumbSkin: "ywhThumbSkin", ywhMarkSkin: "ywhMarkSkin"};
		
		public static function getStyleDefinition():Object
		{
			return defaultStyles;
		}
		
		override protected function handleError(event:Event):void
		{
			clearLoadEvents();
			(loader["contentLoaderInfo"]as EventDispatcher).removeEventListener(Event.INIT, handleInit);
		}
		
		override protected function handleComplete(event:Event):void
		{
			super.handleComplete(event);
			if (content is Bitmap)
			{
				contentClip.addChild(new Bitmap((content as Bitmap).bitmapData, PixelSnapping.AUTO, true));
			}
		}
		
		/**
		 * 此方法来自BaseButton
		 */
		override protected function draw():void
		{
			if (isInvalid(InvalidationType.STYLES))
			{
				drawBackground();
				invalidate(InvalidationType.SIZE, false);
			}
			if (isInvalid(InvalidationType.SIZE))
			{
				drawLayout();
			}
			super.draw();
		}
		
		/**
		 * 此方法来自BaseButton
		 * 经典的换背景的方法，呵呵;
		 */
		protected function drawBackground():void
		{
			var styleName:String = "ywhThumbSkin";
			var bg:DisplayObject = background;
			background = getDisplayObjectInstance(getStyleValue(styleName));
			addChildAt(background, 0);
			if (bg != null && bg != background)
			{
				removeChild(bg);
			}
		}
		
		/**
		 * 此方法来自BaseButton
		 */
		override protected function drawLayout():void
		{
			super.drawLayout();
			background.width = width;
			background.height = height;
		}
	}

}